<?php

namespace app\common\utils;

use app\common\library\Email;
use GuzzleHttp\Client;
use think\Config;

/**
 * 工具类
 */
class Util
{
    /**
     * 快递100实时查询接口
     */
    public static function kuaidi100($com, $nu){
        //参数设置
        $key = 'iUsOjWFh5740';                        // 客户授权key
        $customer = '43FC09AE9205E4ACA71D15F986506F9B';                   // 查询公司编号
        $param = array (
            'com' => $com,             // 快递公司编码
            'num' => $nu,     // 快递单号
            'phone' => '',                // 手机号
            'from' => '',                 // 出发地城市
            'to' => '',                   // 目的地城市
            'resultv2' => '1',            // 开启行政区域解析
            'show' => '0',                // 返回格式：0：json格式（默认），1：xml，2：html，3：text
            'order' => 'desc'             // 返回结果排序:desc降序（默认）,asc 升序
        );
        //请求参数
        $post_data = array();
        $post_data['customer'] = $customer;
        $post_data['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
        $sign = md5($post_data['param'].$key.$post_data['customer']);
        $post_data['sign'] = strtoupper($sign);
        $url = 'https://poll.kuaidi100.com/poll/query.do';    // 实时查询请求地址
        // 发送post请求
        $client = new Client([
            'timeout' => 5,
        ]);
        $response = $client->request('POST', $url, [
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded',
            ],
            'form_params' => $post_data
        ]);
        return json_decode($response->getBody(), true);
    }

    /**
     * 短信宝短信接口
     */
    public static function smsbao($content, $mobile){
        $pwd= md5('wsl123456');
        $url = "http://api.smsbao.com/sms?u=wsl123123&p={$pwd}&m=$mobile&c=$content";
        file_get_contents($url);
    }


    public static function sendEmail($title, $content,$email="troyxx@sina.com"):void{
        $obj = new Email;
        $result = $obj
            ->to($email)
            ->subject($title)
            ->message($content)
            ->send();
    }
}